/*
	This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;


namespace BlindShark.Widgets
{
	public class CheckBox
	{
		public delegate void ChangeValueEvent();
		public ChangeValueEvent ChangeValue;
		
		#region Variables
		string _sPicture;
		
		Rectangle _rctCheckbox;
		Rectangle _rctActiveArea;
		Surface[] _sfCheckbox;
		
		bool _bChecked;
		bool _bEnabled;
		
		#endregion
		
		#region Accesseurs
		public bool Checked
		{
			get {return _bChecked;}
			set
			{
				if (_bChecked!=value)
				{
					_bChecked = value;
					Init();
				}
			}
		}
		
		public Surface Surface
		{
			get {return _sfCheckbox[_bChecked ? 0 : 1];}
		}
		
		public Point Location
		{
			get {return _rctCheckbox.Location;}
			set {_rctCheckbox.Location = value;}
		}
		
		public int X
		{
			get {return _rctCheckbox.X;}
			set {_rctCheckbox.X = value;}
		}
		
		public int Y
		{
			get {return _rctCheckbox.Y;}
			set {_rctCheckbox.Y = value;}
		}
		
		public int Width
		{
			get {return _rctCheckbox.Width;}
		}
		
		public int Height
		{
			get {return _rctCheckbox.Height;}
		}
		
		public int Bottom
		{
			get {return _rctCheckbox.Bottom;}
		}
		
		public int Right
		{
			get {return _rctCheckbox.Right;}
		}
		
		public bool Enabled
		{
			get {return _bEnabled;}
			set {_bEnabled = value;}
		}
		#endregion
		
		public CheckBox(string pictureFile)
		{
			_sPicture = pictureFile;
			_rctCheckbox = new Rectangle(0,0,1,1);
			_rctActiveArea = _rctCheckbox;
			
			_sfCheckbox = new Surface[2];
			_bChecked = false;
			_bEnabled = true;
		}
		
		public void Init()
		{
			Surface _sfGlobal = SurfaceManager.GetSurface(_sPicture);
			_rctCheckbox.Width = _sfGlobal.Width / 2;
			_rctCheckbox.Height = _sfGlobal.Height;
			
			_sfCheckbox[0] = _sfGlobal.CreateSurfaceFromClipRectangle(new Rectangle(0,0,_rctCheckbox.Width, _rctCheckbox.Height));
			_sfCheckbox[1] = _sfGlobal.CreateSurfaceFromClipRectangle(new Rectangle(_rctCheckbox.Width,0,_rctCheckbox.Width, _rctCheckbox.Height));
		}
		
		public void CleanUp()
		{
			_sfCheckbox[0] = null;
			_sfCheckbox[1] = null;
		}
		
		public void HandleEvents()
		{
			if (!_bEnabled)
				return;
			
			Point pm = Mouse.MousePosition;
			if (_rctCheckbox.Contains(pm) && Mouse.IsButtonPressed(MouseButton.PrimaryButton))
			{
				Checked = !Checked;
				if (ChangeValue!=null)
					ChangeValue();
			}
		}
	}
}
